const { request } = require('../../services/request')
const navbarBg = '../../assets/images/navbar-bg.png'

Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarBg, // 导航栏背景图
        channelList: [], // 首页频道数据
        statusBarHeight: 20,
        selectingUser: false,
        goMenu: {},
        userId: undefined,
        scrollBottom: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // 校验token
        let token = wx.getStorageSync('token')
        if (!token) {
            wx.navigateTo({
                url: '/pages/login/index',
                success: () => {},
                fail: (err) => {
                    console.log(err)
                },
            })
        } else {
            let userId = JSON.parse(wx.getStorageSync('userInfo')).userId
            this.setData({ userId: userId })
                // 获取状态栏高度
            const { statusBarHeight, screenHeight, safeArea } = wx.getSystemInfoSync()
            this.statusBarHeight = statusBarHeight
            this.setData({
                scrollBottom: screenHeight - safeArea.bottom + 54
            })
        }

    },
    getMenuList() {
        request('GET', '/applet/menu/list')
            .then((res) => {
                const { data } = res
                console.log(data)
                data.forEach((e) => {
                    e.tags = JSON.parse(e.tags)
                })
                console.log(data)
                this.setData({
                    channelList: data,
                })
                wx.setStorageSync('menuList', data)
            })
            .catch((err) => {})
    },
    handleToChannel(e) {
        let { menuId, title } = this.data.channelList[e.currentTarget.dataset.index]
        wx.navigateTo({
                url: `/pages/channel/index?menuId=${menuId}&title=${title}&userId=${this.data.userId}`,
            })
            //     let { menuId,title } = this.data.channelList[e.currentTarget.dataset.index]
            // this.setData({goMenu:{ menuId,title }})
            //     this.showSelectUser()
    },
    showSelectUser() {
        this.setData({
                selectingUser: true,
            })
            // request('POST', '/applet/answer/user/list')
            // .then((res) => {
            //   this.setData({selectingUser:true})
            //   this.setData({
            //     userList: res.data,
            //   })
            // })
            // .catch()

    },

    handelSelectUser(e) {
        console.log('select user in home', e)
        const { answeUserId, userId } = e.detail
            // wx.navigateTo({
            //   url: `/pages/user-list/index?menuId=${menuId}&title=${title}`,
            // })
        wx.navigateTo({
            url: `/pages/channel/index?title=${this.data.goMenu.title}&menuId=${this.data.goMenu.menuId}&userId=${userId}&answeUserId=${answeUserId}`,
        })
    },
    hideUserSelect() {
        this.setData({ selectingUser: false })
    },
    handelGoTongue() {
        // this.setData({selectingUser:true})
        // this.setData({goMenu:{ menuId:1,title:'智能舌诊' }})
        wx.navigateTo({
            url: `/pages/channel/index?menuId=1&title=GPT舌诊&userId=${this.data.userId}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getMenuList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
})